package com.zl.tby.controller;

import com.zl.tby.core.common.R;
import com.zl.tby.model.TbyEducationResource;
import com.zl.tby.service.ITbyEducationResourceService;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.core.handle.MethodType;
import org.noear.solon.validation.annotation.Valid;
import org.noear.solon.validation.annotation.Validated;

/**
 * 教育资源管理控制器
 * @author changpeng
 * @version 1.0
 * @since 2024/8/6 20:22
 */
@Valid
@Controller
@Mapping("/education_resource")
public class TbyEducationResourceController {

    @Inject
    private ITbyEducationResourceService tbyEducationResourceService;

    /**
     * 保存或新增课程
     * @param entity: 表单对象
     * @return 操作结果
     */
    @Mapping(value = "/course", method = MethodType.POST)
    public R<TbyEducationResource> course(@Validated TbyEducationResource entity) {
        try {
            TbyEducationResource db = this.tbyEducationResourceService.getDetail();
            if (db.getId() == null) {
                return tbyEducationResourceService.add(entity);
            }
            db.updateCourse(entity);
            return tbyEducationResourceService.update(db);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.failed(ex.getMessage());
        }
    }

    /**
     * 保存或新增标本
     * @param entity: 表单对象
     * @return 操作结果
     */
    @Mapping(value = "/specimen", method = MethodType.POST)
    public R<TbyEducationResource> specimen(@Validated TbyEducationResource entity) {
        try {
            TbyEducationResource db = this.tbyEducationResourceService.getDetail();
            if (db.getId() == null) {
                return tbyEducationResourceService.add(entity);
            }
            db.updateSpecimen(entity);
            return tbyEducationResourceService.update(db);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.failed(ex.getMessage());
        }
    }

    /**
     * 保存或新增人员
     * @param entity: 表单对象
     * @return 操作结果
     */
    @Mapping(value = "/person", method = MethodType.POST)
    public R<TbyEducationResource> person(@Validated TbyEducationResource entity) {
        try {
            TbyEducationResource db = this.tbyEducationResourceService.getDetail();
            if (db.getId() == null) {
                return tbyEducationResourceService.add(entity);
            }
            db.updatePerson(entity);
            return tbyEducationResourceService.update(db);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.failed(ex.getMessage());
        }
    }

    /**
     * 获取详情
     * @return 结果
     */
    @Mapping(value = "/detail", method = MethodType.GET)
    public R<TbyEducationResource> get() {
        TbyEducationResource entity = this.tbyEducationResourceService.getDetail();
        return entity == null ? R.failed("数据不存在") : R.ok(entity);
    }

}
